package cn.piesat.client.config;

import cn.piesat.client.constant.consist.AppProperties;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

@Configuration
public class ThreadPoolConfig {

    @Autowired
    private AppProperties appProperties;

    @Bean
    public ExecutorService kafkaTaskThreadPool() {
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("AlgorithmTaskThread-%d")
                .setUncaughtExceptionHandler(exceptionHandler)
                .setDaemon(true)
                .build();
        return Executors.newFixedThreadPool(appProperties.getTaskThreadPoolSize(),threadFactory);
    }
    @Bean
    public ExecutorService kafkaTaskThread01Pool() {
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("AlgorithmTaskThread-%d")
                .setUncaughtExceptionHandler(exceptionHandler)
                .setDaemon(true)
                .build();
        return Executors.newFixedThreadPool(1, threadFactory);
    }
    @Bean
    public ExecutorService clientStatusThreadPool() {
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("client-status-%d")
                .setUncaughtExceptionHandler(exceptionHandler)
                .setDaemon(true)
                .build();
        return Executors.newFixedThreadPool(appProperties.getRedisServicePoolSize(),threadFactory);
    }

    @Bean
    public ExecutorService redisServiceThreePool() {
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        final ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("AlgorithmTaskThread-%d")
                .setUncaughtExceptionHandler(exceptionHandler)
                .setDaemon(true)
                .build();
        return Executors.newFixedThreadPool(10,threadFactory);
    }
}
